package com.guo.core.framework.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ExecutorConfig {

    @Bean
    @ConfigurationProperties(prefix = "biz.consumer.executor")
    public ThreadPoolConfig threadPoolConfig() {
        return new ThreadPoolConfig();
    }

    @Bean("observerExecutor")
    public Executor observerExecutor() {
        ThreadPoolTaskExecutor observerExecutor = new ThreadPoolTaskExecutor();
        ThreadPoolConfig threadPoolConfig = this.threadPoolConfig();
        observerExecutor.setCorePoolSize(threadPoolConfig.getCorePoolSize());
        observerExecutor.setMaxPoolSize(threadPoolConfig.getMaxPoolSize());
        observerExecutor.setQueueCapacity(threadPoolConfig.getQueueCapacity());
        observerExecutor.setThreadNamePrefix(threadPoolConfig.getThreadNamePrefix());
        observerExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        observerExecutor.initialize();

        return observerExecutor;
    }

}
